# -*- coding:utf-8 -*-
# --author： jingfeng 
# time: 2018/12/8


import threading
import multiprocessing # 分布式进程
import multiprocessing.managers  # 分布式进程管理器
import random
import time
import queue
import gevent
import gevent.monkey


class QueueManger(multiprocessing.managers.BaseManager):  # 继承，进程管理共享数据
    pass

def geventgo(data,result):
    print(data * data)
    result.put(data*data)

def threadgo(datalist,result):
    tasklist = []
    for data in datalist:
        tasklist.append(gevent.spawn(geventgo,data,result))

    gevent.joinall(tasklist)


def processgo(ddatalist,result):
    threadlist = []

    for datalist in ddatalist:
        mythread = threading.Thread(target=threadgo,args=(datalist,result))
        mythread.start()
        threadlist.append(mythread)
    for mythread in threadlist:
        mythread.join()



if __name__ == '__main__':
    QueueManger.register('get_task')  # 注册两个函数，方便调用服务器
    QueueManger.register('get_result')

    manger = QueueManger(address=('192.168.1.174', 65034), authkey='000000'.encode('utf-8'))
    manger.connect()  # 连接服务器

    task = manger.get_task()
    result = manger.get_result()

    # 10 个进程
    # 10个 线程
    # 1000 个协程
    try:
        cubelist = []
        for j in range(10):

            arealist = []

            for k in range(10):
                linelist = []
                for l in range(10):
                    data = task.get()
                    print(data)
                    linelist.append(data)
                arealist.append(linelist)
            cubelist.append(arealist)

    except:
        pass

    processlist = []
    for myarealist in cubelist:
        process = multiprocessing.Process(target=processgo, args=(myarealist,result))
        process.start()
        processlist.append(process)
    for i in processlist:
        i.join()